import {
  indexApi
} from '../../../api/index';
import {
  request
} from '../../../utils/request';
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp();
Page({
  data: {
    defaultData: {
      title: "户型管理",
      imageSrc: "/images/back.png",
      isBackToPreviousPage: true
    },
    pageNum: 1,
    pageSize: 10,
    roomTypeList: [], //房型
    HouseTypeList: [], //户型
    roomList: [], //房间
    activeKey: null,
    roomTypeId: null,
    radio: '1',
    selectedHouse: null, //选中的房间
    show: false, //显示下半部分
    typeId: null
  },

  onLoad: async function (options) {
    if (options.programme) {
      await this.getRoomTypeAll();
      const programme = JSON.parse(decodeURIComponent(options.programme));
      console.log("options.programme", programme);
      this.getHouseItemById(programme.houseTypeItemId);
    }
  },

  //获取房型 getHouseTypeApi
  async getRoomTypeAll() {
    try {
      const res = await request({
        url: indexApi.getRoomTypeAllApi(),
        method: "POST",
        data: {
          pageNum: this.data.pageNum,
          pageSize: this.data.pageSize
        }
      });
      this.setData({
        roomTypeList: res.data,
        // activeKey: 0,
        // roomTypeId: res.data[0].id
      });
      // console.log("res", res.data);
      console.log("roomTypeList", this.data.roomTypeList);
      // this.getHouseType();
    } catch (error) {
      console.error("Error:", error);
    }
  },

  async getHouseType(event) {
    console.log("event", event);
    let id;
    if (event) {
      id = event.currentTarget.dataset.dataid;
      console.log("id", event);
      this.setData({
        roomTypeId: id,
        HouseTypeList: [],
      });
      console.log("roomTypeId====", id);
    } else {
      id = this.data.roomTypeId;
      console.log("roomTypeId", id);
    }
    try {
      const res = await request({
        url: indexApi.getHouseTypeApi(),
        method: "POST",
        data: {
          id: id
        }
      });
      const processedData = res.data.map(house => {
        return {
          ...house,
          rooms: house.rooms.map(room => {
            const {
              id,
              ...rest
            } = room;
            return {
              ...rest,
              tid: id,
            };
          })
        };
      });
      this.setData({
        HouseTypeList: processedData,
      });
      console.log("HouseTypeList", this.data.HouseTypeList);
    } catch (error) {
      console.error("Error:", error);
    }
  },

  async getHouseItemById(houseTypeItemId) {
    const id = houseTypeItemId;
    try {
      const res = await request({
        url: indexApi.getHouseItemByIdApi(),
        method: "POST",
        data: {
          id: id
        }
      });
      console.log("默认选中的户型", res.data);
      const activeIndex = this.data.roomTypeList.findIndex(item => item.id === res.data.typeId);
      console.log("选择房型的id", activeIndex);
      this.setData({
        roomTypeId: res.data.typeId,
        activeKey: activeIndex,
        radio: houseTypeItemId,
        show: true,
        selectedHouse: res.data
      });

      console.log("activeKey", this.data.activeKey, this.data.radio);
      this.getHouseType();
      console.log("上个页面传过来的id", res.data);

    } catch (error) {
      console.error("Error:", error);
    }
  },

  onChange(event) {
    const house = event.currentTarget.dataset.house;
    this.setData({
      selectedHouse: house,
      radio: event.detail,
      show: true,
    });
    console.log("selectedHouse", this.data.selectedHouse);
    console.log("show", this.data.show);
  },

  // 确定
  beSure(event) {
    if (this.data.selectedHouse == null) {
      Dialog.confirm({
          title: '您还没有选择户型',
        })
        .then(() => {
          // 用户确认后的逻辑
        })
        .catch(() => {
          // 取消删除的逻辑
        });
    } else {
      Dialog.confirm({
          title: '确定要选择此户型？',
        })
        .then(() => {
          console.log("this.data.selectedHouse", this.data.selectedHouse);
          // app.globalData.selectedHouseType = this.data.selectedHouse;
          // console.log("app.globalData.selectedHouse", app.globalData.selectedHouseType);
          // const eventChannel = this.getOpenerEventChannel();
          // eventChannel.emit('selectedHouseType', {
          //   selectedHouse: this.data.selectedHouse
          // });
          // console.log();
          wx.setStorageSync('selectedHouseType', JSON.stringify(this.data.selectedHouse));
          wx.navigateBack();
        })
        .catch(() => {
          // 取消删除的逻辑
        });
    }
  },


  async beExit() {
    const id = this.data.selectedHouse.id;
    console.log("selectedHouse", this.data.selectedHouse);
    console.log("this.data.selectedHouse.typeId", this.data.selectedHouse.typeId);

    if (this.data.selectedHouse.typeId !== -1) {
      wx.showToast({
        title: "请选择自定义户型",
        icon: "none",
      });
      return;
    }
    Dialog.confirm({
        title: '确定要删除这个户型吗？',
      })
      .then(async () => {
        try {
          const res = await request({
            url: indexApi.delMyHouseApi(),
            method: "POST",
            data: {
              id
            }
          });
          wx.showToast({
            title: "删除成功",
            icon: "none",
          });
          console.log("res.data", res.data);
          this.setData({
            roomTypeId: -1
          });
          this.getHouseType();
        } catch (error) {
          console.error("Error:", error);
        }
      })
      .catch(() => {
        wx.showToast({
          title: "操作已取消",
          icon: "none",
        });
      });
  }


})